package com.taptech.doufu.ui.activity;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.personalcenter.NewMyCollectView;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;

/**
 * Created by longsiyang on 16/4/6.
 */
public class DFKeepActivity extends DiaobaoBaseActivity
{
    public final static int SORT_ORDER_BY_KEEP =0;//按收藏时间排序
    public final static int SORT_ORDER_BY_TIME = 1;//按最新更新时间排序

    public final static String SP_KEEP_ACTIVITY_ORDER = "keep_activity_order";
    public final static String SP_KEEP_ACTIVITY_GUIDE = "keep_activity_guide";
    private FrameLayout framelayout;
    private TextView tvSort;
    private LinearLayout llSort;
    private NewMyCollectView collect;
    private LinearLayout llGuide;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dfkeep);
        initsView();
    }

    private PopupWindow mMorePopupWindow;
    View mMoreView;
    private void showMorePopupWindow()
    {
        if (mMorePopupWindow == null || mMoreView == null)
        {
            makeTopMorePopupWindow();
        }
        mMorePopupWindow.showAtLocation(mMoreView, Gravity.TOP|Gravity.RIGHT , 0,0);
        mMorePopupWindow.setFocusable(true);
        mMorePopupWindow.setOutsideTouchable(true);
        mMorePopupWindow.update();
    }

    private void makeTopMorePopupWindow()
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMoreView = inflater.inflate(R.layout.popup_reader_more_panel, null);
        View lay = mMoreView.findViewById(R.id.lay);
        lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMorePopupWindow != null) {
                    mMorePopupWindow.dismiss();
                }
            }
        });
        LinearLayout sort_update_keep = (LinearLayout) mMoreView.findViewById(R.id.sort_update_keep);
        sort_update_keep.setVisibility(View.VISIBLE);
        sort_update_keep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (collect != null) {
                    collect.orderChange(SORT_ORDER_BY_KEEP);
                    setTvSort(SORT_ORDER_BY_KEEP);
                }
                if (mMorePopupWindow != null) {
                    mMorePopupWindow.dismiss();
                }

                TMAnalysis.event(DFKeepActivity.this, "COLLECT_SORT_BY_KEEP_TIME");
                DiaoBaoSharedPreferences.setSharedPreferencesValueToInt(SP_KEEP_ACTIVITY_ORDER,SORT_ORDER_BY_KEEP,DFKeepActivity.this);
            }
        });
        LinearLayout sort_update_time = (LinearLayout) mMoreView.findViewById(R.id.sort_update_time);
        sort_update_time.setVisibility(View.VISIBLE);
        sort_update_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (collect != null) {
                    collect.orderChange(SORT_ORDER_BY_TIME);
                    setTvSort(SORT_ORDER_BY_TIME);
                }
                if (mMorePopupWindow != null) {
                    mMorePopupWindow.dismiss();
                }
                TMAnalysis.event(DFKeepActivity.this, "COLLECT_SORT_BY_UPDATE_TIME");
                DiaoBaoSharedPreferences.setSharedPreferencesValueToInt(SP_KEEP_ACTIVITY_ORDER,SORT_ORDER_BY_TIME,DFKeepActivity.this);
            }
        });
        View sort_update_time_line = mMoreView.findViewById(R.id.sort_update_time_line);
        sort_update_time_line.setVisibility(View.VISIBLE);
        mMoreView.findViewById(R.id.popup_reader_more_collect).setVisibility(View.GONE);
        mMoreView.findViewById(R.id.popup_page_turning).setVisibility(View.GONE);
        mMorePopupWindow = new PopupWindow(mMoreView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mMorePopupWindow.setAnimationStyle(R.style.Animation_Right_Left);
        mMorePopupWindow.setBackgroundDrawable(new BitmapDrawable());

    }

    private void initsView(){
        int order = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(SP_KEEP_ACTIVITY_ORDER,this,0);
        framelayout=(FrameLayout)findViewById(R.id.frame_dfkeep);
        llGuide = (LinearLayout) findViewById(R.id.llGuide);
        llGuide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                llGuide.setVisibility(View.INVISIBLE);
                DiaoBaoSharedPreferences.setSharedPreferencesValueToBoolean(SP_KEEP_ACTIVITY_GUIDE,false,DFKeepActivity.this);
            }
        });
        setGuideView();
        tvSort = (TextView) findViewById(R.id.tvSort);
        llSort = (LinearLayout) findViewById(R.id.llSort);
        setTvSort(order);
        llSort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMorePopupWindow();
            }
        });
        LayoutInflater mLi = LayoutInflater.from(this);
        View keepview = mLi.inflate(R.layout.new_my_collect_layout, null);
        collect = new NewMyCollectView(keepview, DFKeepActivity.this,order);
        framelayout.addView(keepview);
    }

    private void setGuideView() {
        boolean hasGuide = DiaoBaoSharedPreferences.getSharedPreferencesValueToBoolean(SP_KEEP_ACTIVITY_GUIDE, this, true);
        if (hasGuide) {
            llGuide.setVisibility(View.VISIBLE);
        }else{
            llGuide.setVisibility(View.INVISIBLE);
        }
    }

    private void setTvSort(int order) {
        if (order > 0) {
            tvSort.setText("按更新时间");
        }else{
            tvSort.setText("按收藏时间");
        }
    }

    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv_dfkeep_back:
                DFKeepActivity.this.finish();
                break;
        }
    }
}
